package com.teamc.team.controller;

import com.teamc.team.entity.Object;
import com.teamc.team.response.ResponseData;
import com.teamc.team.response.ResultEnums;
import com.teamc.team.service.IObjectService;
import com.teamc.team.service.ITypeService;
import com.teamc.team.util.ResponseDataUtil;
import com.teamc.team.vo.AdminObjectVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ding
 */
@Controller
public class AdminObjectController {

    private final IObjectService service;
    private final ITypeService typeService;

    @Autowired
    public AdminObjectController(IObjectService service,ITypeService typeService) {
        this.service = service;
        this.typeService = typeService;
    }

    /**
     * 添加ob
     * @return
     */
    @PostMapping("/admin/addObject")
    public String addObject(Object object,Model mo) {
        object.setObjectAddTime(LocalDate.now());
        object.setObjectTypes(typeService.findTypeIdByName(object.getObjectTypes()));
        System.out.println(object);
        try {
            service.addObject(object);
            mo.addAttribute("msg","添加书籍成功");
            return "redirect:/admin";
        } catch (Exception e) {
            mo.addAttribute("msg","添加书籍失败");
            return "redirect:/admin";
        }
    }

    /**
     * 删除ob
     * @param id
     * @return
     */
    @RequestMapping("/admin/delObject/{id}")
    public String delObject(@PathVariable("id") String id, Model mo) {
        try {
            service.delObject(id);
        } catch (Exception e) {
            mo.addAttribute("msg","当前书籍不可删除！");
        }
        return "redirect:/admin";
    }

    /**
     * 查找所有的ob
     * @return
     */
    @GetMapping("/admin/findAllObject")
    @ResponseBody
    public ResponseData findAllObject() {
        List<AdminObjectVO> adminObjectVOS;
        try {
            adminObjectVOS = service.adminFindAllObject();
        } catch (Exception e) {
            return ResponseDataUtil.buildError(e);
        }
        return ResponseDataUtil.buildSuccess(adminObjectVOS);
    }

    /**
     * 修改ob的可借阅权限
     * @param id
     * @return
     */
    @PutMapping("/admin/changeObjectEnabled/{id}/{newEnabled}")
    @ResponseBody
    public ResponseData changeEnabled(@PathVariable("id") String id,@PathVariable("newEnabled") String newEnabled) {
        Object object = new Object();
        object.setObjectId(id);
        object.setObjectEnabled(newEnabled);
        try {
            service.change(object);
            return ResponseDataUtil.buildSuccess(object.getObjectId()+"更新借阅权限失败"+object.getObjectEnabled());
        } catch (Exception e) {
            return ResponseDataUtil.buildError("更新借阅权限失败");
        }
    }

    @PostMapping("/admin/updateBook")
    public String update(Object ob,Model mo) {
        try {
            String typeIdByName = typeService.findTypeIdByName(ob.getObjectTypes());
            ob.setObjectTypes(typeIdByName);
            ob.setObjectAddTime(service.getById(ob.getObjectId()).getObjectAddTime());
            System.out.println(ob);
            int change = service.change(ob);
            if (change != 0) {
                mo.addAttribute("msg","修改成功！");
                return "redirect:/admin";
            }
        } catch (Exception e) {
            mo.addAttribute("msg","哦豁 报错了！");
            return "redirect:/admin";
        }
        mo.addAttribute("msg","修改失败！");
        return "redirect:/admin";
    }

    /**
     * 通过typename查找
     * @param typeName
     * @return
     */
    @GetMapping("/admin/findAllByTypeName/{name}")
    @ResponseBody
    public ResponseData findAllByTypeName(@PathVariable("name") String typeName) {
        return ResponseDataUtil.buildSuccess(service.findAllVOByTypeName(typeName));
    }

    /**
     * 通过物品id查找
     * @param id
     * @return
     */
    @GetMapping("/admin/findUserByObjectID/")
    public String findByObjectID(String id,Model mo) {
        mo.addAttribute("books",service.findByObjectID(id));
        return "admin/book_search_result";
    }

    @RequestMapping("/admin/book/addBook")
    public String addBook() {
        return "/admin/book_add";
    }

    @RequestMapping({"/admin","/admin/book"})
    public String book(Model mo) {
        List<AdminObjectVO> adminObjectVOS = service.adminFindAllObject();
        mo.addAttribute("list",adminObjectVOS);
        return "admin/books";
    }

    @RequestMapping("/admin/Edit/{id}")
    public String edit(@PathVariable("id") String id, Model mo) {
        AdminObjectVO collect = service.adminFindAllObject().stream()
                .filter(x -> x.getObjectId().equals(id))
                .collect(Collectors.toList()).get(0);
        mo.addAttribute("c",collect);
        return "admin/book_edit";
    }
}